package com.superwisdom.controller.client;

import com.github.pagehelper.PageInfo;
import com.superwisdom.annotation.UnManagerLoginStop;
import com.superwisdom.annotation.UnMerchantLoginStop;
import com.superwisdom.domain.client.Merchant;
import com.superwisdom.domain.client.User;
import com.superwisdom.pojo.PageResult;
import com.superwisdom.pojo.Result;
import com.superwisdom.service.client.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 这个类用于对商家操作的前后端交互
 */

@RestController
@RequestMapping("/Merchant")
public class MerchantController {
    @Autowired
    private MerchantService merchantService;

    //添加商家
    @PostMapping("/add")
    @UnManagerLoginStop
    public Result addMerchant(@RequestBody Merchant merchant) throws Exception
    {
        if (merchant.getId() != null)
            throw new Exception("id不为null");
        merchantService.addMerchant(merchant);
        return new Result("添加成功", 200);
    }

    //将用户设置为商家
    @UnManagerLoginStop
    @GetMapping("/setMerchant/{id}")
    public Result setMerchant(@PathVariable("id") Long id)
    {
        merchantService.setMerchent(id);
        return new Result("设置成功",200);
    }

    //分页查询商家列表
    @UnManagerLoginStop
    @GetMapping("/findByPage/{pageSize}/{pageNum}")
    public Result findByPage(@PathVariable("pageSize") int pageSize, @PathVariable("pageNum") int pageNum) {
        List<Merchant> list = merchantService.findAll(pageSize, pageNum);
        PageInfo pageInfo = new PageInfo(list);
        PageResult pageResult = new PageResult(pageInfo.getPageNum(), pageInfo.getPageSize(), (int) pageInfo.getTotal(), list);
        return new Result("查找成功", 200, pageResult);
    }

    //按商家ID查找
    @UnMerchantLoginStop
    @UnManagerLoginStop
    @GetMapping("/findByID/{id}")
    public Result findByID(@PathVariable Long id) throws Exception {
        Merchant merchant = merchantService.findByID(id);
        if (merchant == null)
            throw new Exception("δ�ҵ����̼�");
        return new Result("���ҳɹ�", 200, merchant);
    }

    //修改商家信息
    @UnMerchantLoginStop
    @UnManagerLoginStop
    @PutMapping("/modify")
    public Result modify(@RequestBody Merchant merchant) {
        merchantService.modify(merchant);
        return new Result("�޸ĳɹ�", 200);
    }

    //按商家ID删除
    @UnManagerLoginStop
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id)
    {
        merchantService.delete(id);
        return new Result("删除成功",200);
    }

    //按商家ID注销该商家，删除与其相关的所有信息
    @UnManagerLoginStop
    @UnMerchantLoginStop
    @DeleteMapping("/deleteAll/{id}")
    public Result deleteAll(@PathVariable("id") Long id)
    {
        merchantService.deleteAll(id);
        return new Result("success",200);
    }
}
